var objRegister = {
	register : function(event){
		event.stopPropagation();
		event.preventDefault();
		var emailReg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
		var username = $('#email').val().trim(), password = $('#password').val().trim(), rePassword = $('#rePassword').val().trim();
		$("#errorInfo").html("");
		if (username === '' || password === '' || rePassword === '') {
			$("#errorInfo").html(" the user name and password cannot be empty ");
		}else if(!emailReg.test(username)){
			$("#errorInfo").html(" email format is not correct ");
		}else if(password != rePassword){
			$("#errorInfo").html(" two input password is not consistent  ");
		}else{
			$.post(weburl + 'signup/register', {
				"email" : username,
				"password" : password
			}, function(data) {
				if (data.type == "success") {
					var currUrl = window.location.href, relUrl = document.referrer;
					if(relUrl != ""){
						if(relUrl.indexOf("index")>0){
							window.location.href = "/index";
						}else if(relUrl.indexOf("signup")>0 && currUrl.indexOf("signup")>0){
							window.location.href = "/index";
						}else{
							window.location.href = relUrl;
						}
					}else{
						window.location.href = "/index";
					}
				}else if(data.type == "error"){
					$("#errorInfo").html(data.info);
				}
			}, "json");
		}
	}
};

$(function() {
	$('#register').click(objRegister.register);
	$('#login').click(function(event){
		event.stopPropagation();
		event.preventDefault();
		window.location.href = "/signin";
	});
});
